<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>导航网站 - 管理后台</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: #f5f5f5;
            min-height: 100vh;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px 0;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .header-content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header h1 {
            font-size: 1.8rem;
        }

        .header-right {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .user-info {
            color: rgba(255,255,255,0.9);
            font-size: 14px;
        }

        .back-link, .logout-btn {
            color: white;
            text-decoration: none;
            padding: 8px 16px;
            border: 1px solid rgba(255,255,255,0.3);
            border-radius: 20px;
            transition: all 0.3s ease;
            background: none;
            cursor: pointer;
            font-size: 14px;
        }

        .back-link:hover, .logout-btn:hover {
            background: rgba(255,255,255,0.1);
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 30px 20px;
        }

        .form-section {
            background: white;
            border-radius: 12px;
            padding: 30px;
            margin-bottom: 30px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
        }

        .form-section h2 {
            color: #2c3e50;
            margin-bottom: 20px;
            font-size: 1.4rem;
        }

        .upload-section {
            background: white;
            border-radius: 12px;
            padding: 30px;
            margin-bottom: 30px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
        }

        .file-upload {
            border: 2px dashed #e1e8ed;
            border-radius: 8px;
            padding: 30px;
            text-align: center;
            transition: all 0.3s ease;
            cursor: pointer;
        }

        .file-upload:hover {
            border-color: #667eea;
            background: #f8f9ff;
        }

        .file-upload.dragover {
            border-color: #667eea;
            background: #f0f4ff;
        }

        .file-upload input[type="file"] {
            display: none;
        }

        .upload-icon {
            font-size: 48px;
            color: #bdc3c7;
            margin-bottom: 15px;
        }

        .upload-text {
            color: #7f8c8d;
            margin-bottom: 10px;
        }

        .upload-hint {
            color: #95a5a6;
            font-size: 12px;
        }

        .preview-image {
            max-width: 100%;
            max-height: 200px;
            border-radius: 8px;
            margin-top: 10px;
        }

        .form-section {
            background: white;
            border-radius: 12px;
            padding: 24px;
            margin-bottom: 24px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        }

        .form-section h2 {
            margin: 0 0 20px 0;
            color: #333;
            font-size: 20px;
            font-weight: 600;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
            font-size: 14px;
        }

        .form-group input[type="text"] {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s ease;
            box-sizing: border-box;
        }

        .form-group input[type="text"]:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .form-actions {
            margin-top: 24px;
        }

        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 8px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
        }

        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #34495e;
            font-weight: 500;
        }

        .form-group input,
        .form-group textarea {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e8ed;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s ease;
        }

        .form-group input:focus,
        .form-group textarea:focus {
            outline: none;
            border-color: #667eea;
        }

        .form-group textarea {
            resize: vertical;
            min-height: 80px;
        }

        .form-actions {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 8px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
            text-align: center;
        }

        .btn-primary {
            background: #667eea;
            color: white;
        }

        .btn-primary:hover {
            background: #5a6fd8;
            transform: translateY(-1px);
        }

        .btn-success {
            background: #27ae60;
            color: white;
        }

        .btn-success:hover {
            background: #229954;
        }

        .btn-warning {
            background: #f39c12;
            color: white;
        }

        .btn-warning:hover {
            background: #e67e22;
        }

        .btn-danger {
            background: #e74c3c;
            color: white;
        }

        .btn-danger:hover {
            background: #c0392b;
        }

        .btn-secondary {
            background: #95a5a6;
            color: white;
        }

        .btn-secondary:hover {
            background: #7f8c8d;
        }

        .navigation-list {
            background: white;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
        }

        .list-header {
            background: #f8f9fa;
            padding: 20px 30px;
            border-bottom: 1px solid #e1e8ed;
        }

        .list-header h2 {
            color: #2c3e50;
            font-size: 1.4rem;
        }

        .nav-item {
            padding: 20px 30px;
            border-bottom: 1px solid #e1e8ed;
            transition: background-color 0.3s ease;
        }

        .nav-item:hover {
            background: #f8f9fa;
        }

        .nav-item:last-child {
            border-bottom: none;
        }

        .nav-item-header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            margin-bottom: 10px;
        }

        .nav-item-title {
            font-size: 1.1rem;
            font-weight: 600;
            color: #2c3e50;
            margin-bottom: 5px;
        }

        .nav-item-url {
            color: #667eea;
            text-decoration: none;
            font-size: 0.9rem;
        }

        .nav-item-url:hover {
            text-decoration: underline;
        }

        .nav-item-description {
            color: #7f8c8d;
            font-size: 0.9rem;
            line-height: 1.4;
            margin-top: 8px;
        }

        .nav-item-actions {
            display: flex;
            gap: 8px;
        }

        .nav-item-actions .btn {
            padding: 6px 12px;
            font-size: 12px;
        }

        .message {
            padding: 15px 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-weight: 500;
        }

        .message.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .message.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .loading {
            text-align: center;
            padding: 40px;
            color: #7f8c8d;
        }

        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #7f8c8d;
        }

        .empty-state h3 {
            margin-bottom: 10px;
            color: #95a5a6;
        }

        /* 状态相关样式 */
        .status-badge {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: 500;
            text-align: center;
            min-width: 50px;
        }

        .status-enabled {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .status-disabled {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .status-toggle-btn {
            padding: 4px 8px;
            font-size: 12px;
            border-radius: 4px;
            border: none;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-left: 8px;
        }

        .status-toggle-btn.enable {
            background-color: #28a745;
            color: white;
        }

        .status-toggle-btn.disable {
            background-color: #dc3545;
            color: white;
        }

        .status-toggle-btn:hover {
            opacity: 0.8;
        }

        .nav-item-status {
            display: flex;
            align-items: center;
            margin-bottom: 8px;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .header-content {
                flex-direction: column;
                gap: 15px;
                text-align: center;
            }

            .container {
                padding: 20px 15px;
            }

            .form-section,
            .navigation-list {
                padding: 20px;
            }

            .nav-item {
                padding: 15px 20px;
            }

            .nav-item-header {
                flex-direction: column;
                align-items: flex-start;
                gap: 10px;
            }

            .nav-item-actions {
                width: 100%;
                justify-content: flex-start;
            }

            .form-actions {
                flex-direction: column;
            }

            .btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="header-content">
            <h1>导航网站管理后台</h1>
            <div class="header-right">
                <div class="user-info" id="userInfo">加载中...</div>
                <a href="/" class="back-link">返回首页</a>
                <button class="logout-btn" id="logoutBtn">退出登录</button>
            </div>
        </div>
    </div>

    <div class="container">
        <div id="messageContainer"></div>

        <div class="form-section">
            <h2>网站基本设置</h2>
            <form id="settingsForm">
                <div class="form-group">
                    <label for="siteTitle">网站标题</label>
                    <input type="text" id="siteTitle" name="siteTitle" placeholder="请输入网站标题" maxlength="100">
                </div>
                <div class="form-group">
                    <label for="siteSubtitle">网站副标题</label>
                    <input type="text" id="siteSubtitle" name="siteSubtitle" placeholder="请输入网站副标题" maxlength="200">
                </div>
                <div class="form-actions">
                    <button type="submit" class="btn btn-primary">保存设置</button>
                </div>
            </form>
        </div>

        <div class="upload-section">
            <h2>网站背景设置</h2>
            <div class="file-upload" id="fileUpload">
                <input type="file" id="backgroundFile" accept="image/*">
                <div class="upload-icon">📷</div>
                <div class="upload-text">点击或拖拽上传背景图片</div>
                <div class="upload-hint">支持 JPG、PNG、GIF 格式，最大 5MB</div>
                <img id="previewImage" class="preview-image" style="display: none;">
            </div>
        </div>

        <div class="form-section">
            <h2 id="formTitle">添加新网站</h2>
            <form id="navigationForm">
                <input type="hidden" id="editId" value="">
                
                <div class="form-group">
                    <label for="title">网站标题 *</label>
                    <input type="text" id="title" name="title" required placeholder="请输入网站标题">
                </div>

                <div class="form-group">
                    <label for="url">网站链接 *</label>
                    <input type="url" id="url" name="url" required placeholder="https://example.com">
                </div>

                <div class="form-group">
                    <label for="description">网站描述</label>
                    <textarea id="description" name="description" placeholder="请输入网站描述（可选）"></textarea>
                </div>

                <div class="form-group">
                    <label for="status">状态</label>
                    <select id="status" name="status">
                        <option value="enabled">启用</option>
                        <option value="disabled">禁用</option>
                    </select>
                </div>

                <div class="form-actions">
                    <button type="submit" class="btn btn-primary" id="submitBtn">添加网站</button>
                    <button type="button" class="btn btn-secondary" id="cancelBtn" style="display: none;">取消编辑</button>
                </div>
            </form>
        </div>

        <div class="navigation-list">
            <div class="list-header">
                <h2>网站列表</h2>
            </div>
            <div id="navigationList">
                <div class="loading">正在加载数据...</div>
            </div>
        </div>
    </div>

    <script>
        class AdminApp {
            constructor() {
                this.navigationData = [];
                this.editingId = null;
                this.currentUser = null;
                
                this.form = document.getElementById('navigationForm');
                this.messageContainer = document.getElementById('messageContainer');
                this.navigationList = document.getElementById('navigationList');
                this.formTitle = document.getElementById('formTitle');
                this.submitBtn = document.getElementById('submitBtn');
                this.cancelBtn = document.getElementById('cancelBtn');
                
                this.init();
            }

            async init() {
                 // 检查登录状态
                 const isAuthenticated = await this.checkAuthStatus();
                 if (!isAuthenticated) return;

                 await this.loadNavigationData();
                 await this.loadSiteSettings();
                 this.setupEventListeners();
                 this.renderNavigationList();
                 this.initFileUpload();
             }

            async checkAuthStatus() {
                try {
                    const response = await fetch('/api/auth/status');
                    const data = await response.json();
                    
                    if (!data.isLoggedIn || data.user.role !== 'admin') {
                        // 未登录或非管理员，跳转到登录页
                        window.location.href = '/login';
                        return false;
                    }
                    
                    this.currentUser = data.user;
                    document.getElementById('userInfo').textContent = `欢迎，${data.user.username}`;
                    return true;
                } catch (error) {
                    console.error('检查登录状态失败:', error);
                    window.location.href = '/login';
                    return false;
                }
            }

            async logout() {
                try {
                    const response = await fetch('/api/logout', {
                        method: 'POST'
                    });
                    
                    if (response.ok) {
                        window.location.href = '/login';
                    } else {
                        this.showMessage('登出失败', 'error');
                    }
                } catch (error) {
                    console.error('登出失败:', error);
                    this.showMessage('登出失败', 'error');
                }
            }

            initFileUpload() {
                const fileUpload = document.getElementById('fileUpload');
                const fileInput = document.getElementById('backgroundFile');
                const previewImage = document.getElementById('previewImage');

                // 点击上传
                fileUpload.addEventListener('click', () => {
                    fileInput.click();
                });

                // 文件选择
                fileInput.addEventListener('change', (e) => this.handleFileSelect(e));

                // 拖拽上传
                fileUpload.addEventListener('dragover', (e) => {
                    e.preventDefault();
                    fileUpload.classList.add('dragover');
                });

                fileUpload.addEventListener('dragleave', () => {
                    fileUpload.classList.remove('dragover');
                });

                fileUpload.addEventListener('drop', (e) => {
                    e.preventDefault();
                    fileUpload.classList.remove('dragover');
                    const files = e.dataTransfer.files;
                    if (files.length > 0) {
                        fileInput.files = files;
                        this.handleFileSelect({ target: { files } });
                    }
                });
            }

            handleFileSelect(event) {
                const file = event.target.files[0];
                if (!file) return;

                // 验证文件类型
                if (!file.type.startsWith('image/')) {
                    this.showMessage('请选择图片文件', 'error');
                    return;
                }

                // 验证文件大小
                if (file.size > 5 * 1024 * 1024) {
                    this.showMessage('文件大小不能超过5MB', 'error');
                    return;
                }

                // 预览图片
                const reader = new FileReader();
                reader.onload = (e) => {
                    const previewImage = document.getElementById('previewImage');
                    previewImage.src = e.target.result;
                    previewImage.style.display = 'block';
                };
                reader.readAsDataURL(file);

                // 上传文件
                this.uploadBackgroundImage(file);
            }

            async uploadBackgroundImage(file) {
                const formData = new FormData();
                formData.append('background', file);

                try {
                    const response = await fetch('/api/upload/background', {
                        method: 'POST',
                        body: formData
                    });

                    const data = await response.json();

                    if (response.ok) {
                        this.showMessage('背景图片上传成功', 'success');
                    } else {
                        this.showMessage(data.error || '上传失败', 'error');
                    }
                } catch (error) {
                    console.error('上传失败:', error);
                    this.showMessage('上传失败，请稍后重试', 'error');
             }
         }

         async loadSiteSettings() {
             try {
                 const response = await fetch('/api/settings');
                 const settings = await response.json();
                 
                 // 填充表单
                 document.getElementById('siteTitle').value = settings.site_title || '我的导航网站';
                 document.getElementById('siteSubtitle').value = settings.site_subtitle || '收集和分享优质网站资源';
             } catch (error) {
                 console.error('加载网站设置失败:', error);
             }
         }

         async handleSettingsSubmit() {
             const siteTitle = document.getElementById('siteTitle').value.trim();
             const siteSubtitle = document.getElementById('siteSubtitle').value.trim();
             
             if (!siteTitle) {
                 this.showMessage('网站标题不能为空', 'error');
                 return;
             }
             
             try {
                 const response = await fetch('/api/settings', {
                     method: 'PUT',
                     headers: {
                         'Content-Type': 'application/json'
                     },
                     body: JSON.stringify({
                         settings: {
                             site_title: siteTitle,
                             site_subtitle: siteSubtitle
                         }
                     })
                 });
                 
                 const data = await response.json();
                 
                 if (response.ok) {
                     this.showMessage('网站设置保存成功', 'success');
                 } else {
                     this.showMessage(data.error || '保存失败', 'error');
                 }
             } catch (error) {
                 console.error('保存网站设置失败:', error);
                 this.showMessage('保存失败，请稍后重试', 'error');
             }
         }

            async loadNavigationData() {
                try {
                    const response = await fetch('/api/navigation');
                    if (!response.ok) {
                        throw new Error('网络请求失败');
                    }
                    this.navigationData = await response.json();
                } catch (error) {
                    console.error('加载数据失败:', error);
                    this.showMessage('加载数据失败，请检查网络连接', 'error');
                }
            }

            setupEventListeners() {
                 this.form.addEventListener('submit', (e) => {
                     e.preventDefault();
                     this.handleFormSubmit();
                 });

                 document.getElementById('cancelBtn').addEventListener('click', () => {
                     this.cancelEdit();
                 });

                 document.getElementById('logoutBtn').addEventListener('click', () => {
                     this.logout();
                 });

                 // 网站设置表单
                 document.getElementById('settingsForm').addEventListener('submit', (e) => {
                     e.preventDefault();
                     this.handleSettingsSubmit();
                 });
             }

            async handleSubmit() {
                const formData = new FormData(this.form);
                const data = {
                    title: formData.get('title').trim(),
                    url: formData.get('url').trim(),
                    description: formData.get('description').trim()
                };

                if (!data.title || !data.url) {
                    this.showMessage('请填写必填字段', 'error');
                    return;
                }

                try {
                    let response;
                    if (this.editingId) {
                        response = await fetch(`/api/navigation/${this.editingId}`, {
                            method: 'PUT',
                            headers: {
                                'Content-Type': 'application/json'
                            },
                            body: JSON.stringify(data)
                        });
                    } else {
                        response = await fetch('/api/navigation', {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json'
                            },
                            body: JSON.stringify(data)
                        });
                    }

                    if (!response.ok) {
                        throw new Error('操作失败');
                    }

                    const message = this.editingId ? '更新成功' : '添加成功';
                    this.showMessage(message, 'success');
                    
                    this.form.reset();
                    this.cancelEdit();
                    await this.loadNavigationData();
                    this.renderNavigationList();
                } catch (error) {
                    console.error('操作失败:', error);
                    this.showMessage('操作失败，请重试', 'error');
                }
            }

            async handleFormSubmit() {
                const title = document.getElementById('title').value.trim();
                let url = document.getElementById('url').value.trim();
                const description = document.getElementById('description').value.trim();
                const status = document.getElementById('status').value;

                if (!title || !url) {
                    this.showMessage('请填写必填字段', 'error');
                    return;
                }

                // 处理 ~ 符号替换为当前域名和端口
                if (url.includes('~')) {
                    const currentHost = window.location.hostname;
                    const currentProtocol = window.location.protocol;
                    url = url.replace(/~/g, currentHost);
                    
                    // 确保URL以协议开头
                    if (!url.startsWith('http://') && !url.startsWith('https://')) {
                        url = currentProtocol + '//' + url;
                    }
                }

                // 验证URL格式
                try {
                    new URL(url);
                } catch {
                    this.showMessage('请输入有效的网站链接', 'error');
                    return;
                }

                const data = { title, url, description, status };

                try {
                    let response;
                    if (this.editingId) {
                        // 更新现有项目
                        response = await fetch(`/api/navigation/${this.editingId}`, {
                            method: 'PUT',
                            headers: {
                                'Content-Type': 'application/json'
                            },
                            body: JSON.stringify(data)
                        });
                    } else {
                        // 添加新项目
                        response = await fetch('/api/navigation', {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json'
                            },
                            body: JSON.stringify(data)
                        });
                    }

                    if (!response.ok) {
                        throw new Error('操作失败');
                    }

                    const message = this.editingId ? '更新成功' : '添加成功';
                    this.showMessage(message, 'success');
                    
                    this.form.reset();
                    this.cancelEdit();
                    await this.loadNavigationData();
                    this.renderNavigationList();
                } catch (error) {
                    console.error('操作失败:', error);
                    this.showMessage('操作失败，请重试', 'error');
                }
            }

            async deleteItem(id) {
                if (!confirm('确定要删除这个网站吗？')) {
                    return;
                }

                try {
                    const response = await fetch(`/api/navigation/${id}`, {
                        method: 'DELETE'
                    });

                    if (!response.ok) {
                        throw new Error('删除失败');
                    }

                    this.showMessage('删除成功', 'success');
                    await this.loadNavigationData();
                    this.renderNavigationList();
                } catch (error) {
                    console.error('删除失败:', error);
                    this.showMessage('删除失败，请重试', 'error');
                }
            }

            async toggleStatus(id, newStatus) {
                try {
                    const response = await fetch(`/api/navigation/${id}/status`, {
                        method: 'PATCH',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ status: newStatus })
                    });

                    if (!response.ok) {
                        throw new Error('状态切换失败');
                    }

                    const data = await response.json();
                    this.showMessage(data.message, 'success');
                    await this.loadNavigationData();
                    this.renderNavigationList();
                } catch (error) {
                    console.error('状态切换失败:', error);
                    this.showMessage('状态切换失败，请重试', 'error');
                }
            }

            editItem(item) {
                this.editingId = item.id;
                
                document.getElementById('editId').value = item.id;
                document.getElementById('title').value = item.title;
                
                // 处理URL显示，将当前域名替换为 ~ 符号（可选功能）
                let displayUrl = item.url;
                const currentHost = window.location.hostname;
                if (displayUrl.includes(currentHost)) {
                    displayUrl = displayUrl.replace(new RegExp(currentHost, 'g'), '~');
                }
                
                document.getElementById('url').value = displayUrl;
                document.getElementById('description').value = item.description || '';
                document.getElementById('status').value = item.status || 'enabled';
                
                this.formTitle.textContent = '编辑网站';
                this.submitBtn.textContent = '更新网站';
                this.cancelBtn.style.display = 'inline-block';
                
                // 滚动到表单
                this.form.scrollIntoView({ behavior: 'smooth' });
            }

            cancelEdit() {
                this.editingId = null;
                this.form.reset();
                
                this.formTitle.textContent = '添加新网站';
                this.submitBtn.textContent = '添加网站';
                this.cancelBtn.style.display = 'none';
            }

            renderNavigationList() {
                if (this.navigationData.length === 0) {
                    this.navigationList.innerHTML = `
                        <div class="empty-state">
                            <h3>暂无网站数据</h3>
                            <p>点击上方表单添加第一个网站</p>
                        </div>
                    `;
                    return;
                }

                const listHtml = this.navigationData.map(item => {
                    const status = item.status || 'enabled';
                    const statusText = status === 'enabled' ? '启用' : '禁用';
                    const statusClass = status === 'enabled' ? 'status-enabled' : 'status-disabled';
                    const toggleText = status === 'enabled' ? '禁用' : '启用';
                    const toggleClass = status === 'enabled' ? 'disable' : 'enable';
                    const toggleStatus = status === 'enabled' ? 'disabled' : 'enabled';
                    
                    return `
                        <div class="nav-item">
                            <div class="nav-item-header">
                                <div>
                                    <div class="nav-item-title">${this.escapeHtml(item.title)}</div>
                                    <a href="${item.url}" class="nav-item-url" target="_blank" rel="noopener noreferrer">
                                        ${this.escapeHtml(item.url)}
                                    </a>
                                    <div class="nav-item-status">
                                        <span class="status-badge ${statusClass}">${statusText}</span>
                                        <button class="status-toggle-btn ${toggleClass}" onclick="adminApp.toggleStatus(${item.id}, '${toggleStatus}')">
                                            ${toggleText}
                                        </button>
                                    </div>
                                </div>
                                <div class="nav-item-actions">
                                    <button class="btn btn-warning" onclick="adminApp.editItem(${JSON.stringify(item).replace(/"/g, '&quot;')})">
                                        编辑
                                    </button>
                                    <button class="btn btn-danger" onclick="adminApp.deleteItem(${item.id})">
                                        删除
                                    </button>
                                </div>
                            </div>
                            ${item.description ? `<div class="nav-item-description">${this.escapeHtml(item.description)}</div>` : ''}
                        </div>
                    `;
                }).join('');

                this.navigationList.innerHTML = listHtml;
            }

            showMessage(message, type) {
                const messageEl = document.createElement('div');
                messageEl.className = `message ${type}`;
                messageEl.textContent = message;
                
                this.messageContainer.innerHTML = '';
                this.messageContainer.appendChild(messageEl);
                
                setTimeout(() => {
                    messageEl.remove();
                }, 5000);
            }

            escapeHtml(text) {
                const div = document.createElement('div');
                div.textContent = text;
                return div.innerHTML;
            }
        }

        // 初始化应用
        let adminApp;
        document.addEventListener('DOMContentLoaded', () => {
            adminApp = new AdminApp();
        });
    </script>
</body>
</html>